/**
 * 
 */
package org.gigi.security.service.base;

import org.gigi.core.Page;
import org.gigi.security.domain.User;
import org.gigi.security.repository.UserRepository;
import org.gigi.security.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 基本的用户服务层实现
 *
 * @author Eugene Wang 2011-5-28
 * @see
 */
@Service
@Transactional(readOnly = true)
public class DefaultUserService implements UserService {
	@Autowired
	private UserRepository userRepository;
	/* (non-Javadoc)
	 * @see org.gigi.security.service.UserService#create(org.gigi.security.domain.User)
	 */
	@Transactional
	public void create(User user) {
		user.setPassword(user.getUsername());
		userRepository.create(user);
	}
	/* (non-Javadoc)
	 * @see org.gigi.security.service.UserService#getPage(org.gigi.core.PageArgument)
	 */
	public Page<User> getPage(Page<User> page) {
		return userRepository.getPage(page);
	}
	/* (non-Javadoc)
	 * @see org.gigi.security.service.UserService#remove(java.lang.Long)
	 */
	@Transactional
	public void delete(String username) {
		User user = userRepository.findByUsername(username);
		userRepository.delete(user);
	}
	/* (non-Javadoc)
	 * @see org.gigi.security.service.UserService#update(org.gigi.security.domain.User)
	 */
	@Transactional
	public void update(User user) {
		User dbUser = userRepository.findByUsername(user.getUsername());
		BeanUtils.copyProperties(user, dbUser, new String[] {"password"});
		userRepository.update(dbUser);
	}
	/* (non-Javadoc)
	 * @see org.gigi.security.service.UserService#get(java.lang.String)
	 */
	public User findByUsername(String username) {
		return userRepository.findByUsername(username);
	}
}
